import pymysql
import pymysql.cursors
from common.read_config import ReadConfig
from common.project_path import *
db_config=eval(ReadConfig().read_config(config_path,"DB","db_config"))

class HandleMysql:

    def __init__(self):
        #连接数据库
        self.db=pymysql.connect(
            host=db_config['host'],
            user=db_config['user'],
            password=db_config['password'],
            database=db_config['database'],
            port=db_config['port'],
            cursorclass=pymysql.cursors.DictCursor
        )
        # 创建游标
        self.cursor=self.db.cursor()

    def get_count(self,sql):
        count=self.cursor.execute(sql)
        return count

    def get_one_data(self,sql):
        self.cursor.execute(sql)
        return self.cursor.fetchone()

    def get_many_data(self,sql,size=None):
        self.cursor.execute(sql)
        if size:
            return self.cursor.fetchmany(size)
        else:
            return self.cursor.fetchall()
    def close_db(self):
        self.cursor.close()
        self.db.close()

if __name__ == '__main__':
    db_connect=HandleMysql()
    sql="select * from member where mobile_phone='15128288928'"
    count=db_connect.get_count(sql)
    print(count)
    db_connect.close_db()